package com.cangmaomao.lib.utils

import android.graphics.Bitmap
import android.support.v4.app.Fragment
import com.cangmaomao.lib.utils.SPUtil.create
import java.io.File
import java.io.FileOutputStream

object FileUtils {


    //生成文件文件夹
    fun Fragment.createFiles(loginId: String) {
        val accountFile = activity!!.externalCacheDir.absolutePath + "/pen/" + loginId
        val file = File(accountFile)
        if (!file.exists())
            file.mkdirs()
    }

    //创建分类文件夹
    fun Fragment.createClazzFiles(filesName: String, clazz: String): String {
        val loginId = create("User").get("account", "")
        val clazzFile = activity!!.externalCacheDir.absolutePath + "/pen/" + loginId + "/" + filesName + "/" + clazz
        val file = File(clazzFile)
        if (!file.exists()) {
            file.mkdirs()
        }
        return clazzFile
    }

    //读取文件夹下面的文件
    fun Fragment.readFiles(): Array<File>? {
        var filesList: Array<File>? = null
        val loginId = create("User").get("account", "")
        val files = activity!!.externalCacheDir.absolutePath + "/pen/" + loginId
        val file = File(files)
        if (!file.exists()) {
            file.mkdirs()
        } else {
            filesList = file.listFiles()
        }
        return filesList!!
    }


    //读取文件夹下面的文件
    fun Fragment.readFiles(path: String): Array<File>? {
        var filesList: Array<File>? = null
        val loginId = create("User").get("account", "")
        val files = activity!!.externalCacheDir.absolutePath + "/pen/" + loginId + "/" + path
        val file = File(files)
        if (!file.exists()) {
            file.mkdirs()
        } else {
            filesList = file.listFiles()
        }
        return filesList!!
    }


    //保存画板内容
    fun saveDraw(path: String, name: String, bitmap: Bitmap?) {
        val fileName = "$name.png"
        val file = File(path, fileName)
        val fos = FileOutputStream(file)
        if (bitmap == null) {
            return
        }
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos)
        fos.flush()
        fos.close()
    }

    //刪除指定文件夾
    fun delFolder(path: String) {
        try {
            val files = File(path)
            if (files.exists()) {
                val s = files.listFiles()
                s.forEach {
                    if (it.isFile) {
                        it.delete()
                    } else if (it.isDirectory) {
                        val ss = it.listFiles()
                        if (ss.isNotEmpty()) {
                            ss.forEach {
                                it.delete()
                            }
                        }
                        it.delete()
                    }
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    //删除指定文件
    fun delFile(nameList: String) {
        try {
            val file = File(nameList)
            if (file.exists() && file.isFile) {
                file.delete()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    //递归删除文件夹下面所有的文件与文件夹
    fun delDir(path: String): Boolean {
        val file = File(path)
        if (file.exists()) {
            val files = file.listFiles()
            if (!files.isEmpty()) {
                files.forEach {
                    if (it.isDirectory) {
                        delDir(it.absolutePath)
                    } else {
                        it.delete()
                    }
                }
            }
            return file.delete()

        }
        return false
    }


    //修改文件夹名称
    fun updateFileName(path: String, name: String): Boolean {
        val file = File(path)
        if (file.exists()) {
            val rootPath = file.parent
            val newFile = File(rootPath + File.separator + name)
            return file.renameTo(newFile)
        }
        return false
    }


}


